// 题目store类
import { defineStore } from "pinia";
import {
  type qstnType,
  store,
  router,
  resetRouter,
  routerArrays,
  storageLocal
} from "../utils";
import { type AddQstnResult, addQstn } from "@/api/qstn";

export const useQstnStore = defineStore("useQstnStore", {
  state: (): qstnType => ({
    // 头像
    qstn_id: storageLocal().getItem<qstnType>("useQstnStore")?.qstn_id ?? "",
    qstn_title:
      storageLocal().getItem<qstnType>("useQstnStore")?.qstn_title ?? ""
  }),
  actions: {
    /** 题目编号 */
    SET_QSTN_ID(qstn_id: number) {
      this.qstn_id = qstn_id;
    },
    SET_QSTN_TITLE(qstn_title: string) {
      this.qstn_title = qstn_title;
    },
    // 方法之间以逗号隔开
    // 添加题目
    async doAddQstn(formData) {
      return new Promise<AddQstnResult>((resolve, reject) => {
        addQstn(formData)
          .then(data => {
            resolve(data);
          })
          .catch(error => {
            reject(error);
          });
      });
    }
  }
});

export function useQstnStoreHook() {
  return useQstnStore(store);
}
